#include "bmp.h"

void printBMPFileHeader(BMP *bmp){

    printf("-----FILE HEADER------\n");
    printf("BfType: %x\n",bmp->fileHeader.Bftype);
    printf("BfSize: %u\n",bmp->fileHeader.BfSize);
    printf("BfReser1: %u\n",bmp->fileHeader.BfReser1);
    printf("BfReser2: %u\n",bmp->fileHeader.BfReser2);
    printf("BfOffSetBits: %u\n",bmp->fileHeader.BfOffSet);

    printf("-----BMP  HEADER------\n");
    printf("BiSize: %u\n",bmp->bmpHeader.BiSize);
    printf("BiWidth: %u\n",bmp->bmpHeader.BiWidth);
    printf("BiHeight: %u\n",bmp->bmpHeader.BiHeight);
    printf("BiPlanes: %u\n",bmp->bmpHeader.BiPlanes);
    printf("BiBitCount: %u\n",bmp->bmpHeader.BiBitCount);
    printf("BiCompress: %u\n",bmp->bmpHeader.BiCompress);
    printf("BiSizeImage: %u\n",bmp->bmpHeader.BiSizeImag);
    printf("BiXPPMeter: %u\n",bmp->bmpHeader.BXPPMeter);
    printf("BiYPPMeter: %u\n",bmp->bmpHeader.BYPPMeter);
    printf("BiClrUsed: %u\n",bmp->bmpHeader.BiClrUsed);
    printf("BiClrImport: %u\n",bmp->bmpHeader.BiClrImport);
}

int readHeaderBMPFile(BMP *bmp,FILE *arq){
    if( !arq )
        return 0;

    unsigned char data[HEADER_BMP_SIZE+HEADER_FILE_SIZE];
    fread(data,sizeof(unsigned char),HEADER_BMP_SIZE+HEADER_FILE_SIZE,arq);

    memset(bmp,0,sizeof(BMP_FILE_HEADER)+sizeof(BMP_BITMAP_HEADER));
    memcpy(&(bmp->fileHeader),data,sizeof(BMP_FILE_HEADER));
    memcpy(&(bmp->bmpHeader),data+HEADER_FILE_SIZE,HEADER_BMP_SIZE);

    return 1;
}

int readImageBMP(BMP *bmp,FILE *arq){
    if(!arq)
        return 0;

    unsigned int size = bmp->bmpHeader.BiSizeImag;
    bmp->image = (char *)malloc(size);
    int x = 0;

    fseek(arq,HEADER_BMP_SIZE+HEADER_FILE_SIZE,SEEK_SET);

    x = fread(bmp->image,sizeof(char),size,arq);
    while( x < size)
        x += fread(bmp->image+x,sizeof(char),size,arq);

    return 1;
}
